﻿using log4net;
using Mangox.Common;
using Mangox.Common.Tookit;
using Mangox.Model.MangoxDB;
using Mangox.Model.MySql;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Mangox.BLL.Mangox
{
    public class KongTouBLL
    {
        protected static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static string className = (new StackTrace()).GetFrame(1).GetMethod().ReflectedType.Name + "/";

        /// <summary>
        /// 添加空投信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool AddKongTou(m_kongtou param)
        {
            var flag = false;
            try
            {
                log.Info($"添加空投信息-{JsonCommon.ToJson(param)}");

                using (AirdropEntities dbContext = new AirdropEntities())
                {
                    var temp = new m_kongtou();
                    ObjectReflection.AutoMapping(param, temp);

                    dbContext.m_kongtou.Add(temp);
                    flag = dbContext.SaveChanges() > 0;
                }
            }
            catch (Exception ex)
            {
                log.Error(className + $"添加空投信息失败 param-{JsonCommon.ToJson(param)} fail-{ex}");
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 根据微信BTC/ETH获取用户
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<m_kongtou> GetKongTouByAddress(string type, string address)
        {
            List<m_kongtou> list = null;
            using (AirdropEntities dbContext = new AirdropEntities())
            {
                try
                {
                    var obj = dbContext.m_kongtou.Where(t => true);
                    //if (type == "BTC")
                    //{
                    //    obj = obj.Where(u => u.BTCAddress == address);
                    //}
                    //else if (type == "ETH")
                    //{
                    //    obj = obj.Where(u => u.ETHAddress == address);
                    //}

                    obj = obj.Where(u => u.AddressType == type && u.BitAddress == address);

                    var tempList = obj.ToList() as List<m_kongtou>;

                    if (tempList != null && tempList.Count > 0)
                    {
                        list = tempList;
                    }
                }
                catch (Exception ex)
                {
                    log.Error(className + $"获取空投信息失败 address-{address} fail-{ex}");
                }
            }
            return list;
        }

    }
}
